package org.mysofts.eclipseman.launch;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.mysofts.eclipseman.Eclipse;
import org.mysofts.eclipseman.EclipseInstallations;
import org.mysofts.eclipseman.launch.util.AbstractLauncher;
import org.mysofts.eclipseman.util.MessageProvider;

/**
 * Lance un eclipse.
 * 
 * @author reda
 * 
 * @version $Revision: 1.0 $
 */
public final class EclipseRun extends AbstractLauncher { // $codepro.audit.disable
															// declareDefaultConstructors
	/**
	 * Nom du script.
	 */
	public static final String SCRIPT_NAME = "eclipse-run";

	/**
	 * Nom de l'eclipse a lancer.
	 */
	private String eclipseName;
	/**
	 * Eclipse à lancer.
	 */
	private Eclipse eclipse;

	/**
	 * Constructeur.
	 * 
	 * @param args
	 *            arguments.
	 */
	public EclipseRun(final String[] args) {
		super(MessageFormat.format("{0} <ECLIPSE_TO_LAUNCH>", SCRIPT_NAME),
				args);
	}

	/**
	 * Method parseArguments.
	 * 
	 * @param args
	 *            String[]
	 * 
	 * @throws ParseException
	 *             Erreur de parsing.
	 */
	@Override
	protected void parseArguments(final String[] args) throws ParseException {
		if (args.length != 1) {
			throw new ParseException(MessageFormat.format(
					MessageProvider.get("logs").sub("launchers").sub("errors")
							.sub("argerror").value(), 1));
		} // end if

		this.eclipseName = args[0];
	}

	/**
	 * Method parseOptions.
	 * 
	 * @param commandLine
	 *            CommandLine
	 * 
	 * @throws ParseException
	 *             Erreur de parsing.
	 */
	@Override
	protected void parseOptions(final CommandLine commandLine)
			throws ParseException {
		return; // $codepro.audit.disable unnecessaryReturn
	}

	/**
	 * Method createOptions.
	 * 
	 * @param optlist
	 *            List<Option>
	 */
	@Override
	protected void createOptions(final List<Option> optlist) {
		return; // $codepro.audit.disable unnecessaryReturn
	}

	/**
	 * Identifie l'eclipse à lancer.
	 */
	private void identifyEclipse() {
		this.eclipse = EclipseInstallations.get().getEclipse(this.eclipseName);
		if (this.eclipse == null) {
			super.functionnalError(MessageFormat.format(
					MessageProvider.get("logs").sub("launcher").sub("run")
							.sub("badname").value(), this.eclipseName));
		} // end if
	}

	/**
	 * Execute l'eclipse.
	 */
	private void execute() {
		try {
			System.out.print(MessageFormat.format(MessageProvider.get("logs")
					.sub("launcher").sub("run").sub("during").value(),
					this.eclipseName));
			this.eclipse.execute();
			System.out.println(OK);
		} catch (IOException e) {
			System.out.println(NOK);
			super.functionnalError(e.getMessage());
		}
	}

	/**
	 * Main.
	 * 
	 * @param args
	 *            arguments.
	 */
	public static void main(final String[] args) {
		final EclipseRun obj = new EclipseRun(args);
		obj.identifyEclipse();
		obj.execute();
	}

}
